/**
 * @author yz
 */
CodePanel = function(config){

    this.content = config.content || "<!--Code here-->";
    
    this.lineNumbers = config.lineNumbers || false;
    
    this.jspath = config.jspath || 'codemirror/js/';
    
    this.parserfile = config.parserfile || ["parsexml.js", "parsecss.js", "tokenizejavascript.js", "parsejavascript.js", "parsehtmlmixed.js"];
    
    this.stylesheet = config.stylesheet || ["codemirror/css/xmlcolors.css", "codemirror/css/jscolors.css", "codemirror/css/csscolors.css", "css/codepanel.css"];
    
    this.autoMatchParens = config.autoMatchParens || false;
    
    this.editor = null;
    
    config.tbar = [{
        text: 'Save and Run',
		iconCls:'icon-save-run',
		scope:this,
        handler: function(){
			this.saveRun();
        }
    },{
		text: 'Give up changes',
		iconCls:'icon-refresh',
		scope:this,
		handler:function(){
			this.refreshCode();
		}
	},{
		text: 'Undo',
		iconCls:'icon-undo',
		scope:this,
		handler:function(){
			this.undoEdit();
		}
	},{
		text: 'Redo',
		iconCls:'icon-redo',
		scope:this,
		handler:function(){
			this.redoEdit();
		}
	}]
    
    CodePanel.superclass.constructor.call(this, config);	
    
}

Ext.extend(CodePanel, Ext.Panel, {

    afterRender: function(){
    
        CodePanel.superclass.afterRender.call(this);
        
        this.editor = new CodeMirror(this.body, {
        
            content: this.content,
            
            height: "100%",
            
            lineNumbers: this.lineNumbers,
            
            parserfile: this.parserfile,
            
            stylesheet: this.stylesheet,
            
            path: this.jspath,
            
            autoMatchParens: this.autoMatchParens
			
        });
		
		this.content = this.editor.getCode();
        
    },
    
    getCode: function(){
		if (this.editor != null) {
			return this.editor.getCode();
		}
		else {
			return this.content;
		}
    },
    setCode: function(value){
		
		//Ext.log('set code value');
        if (this.editor != null) {
			//Ext.log('setcode in editor');
			this.editor.setCode(value);
			
			this.content = this.editor.getCode();
		}
		else{
			
			this.content = value;
		}	
    },
	saveRun:function(){
		this.content = this.editor.getCode();
		this.fireEvent("saverun");
	},
	refreshCode:function(){
		this.editor.setCode(this.content);
	},
	undoEdit:function(){
		this.editor.undo();
	},
	redoEdit:function(){
		this.editor.redo();
	}
});


Ext.reg("mashuproom.codepanel", CodePanel);
